﻿using CPPEI.Coolzon.Designtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    public class ImageSourceArrayModel :INotifyPropertyChanged
    {
        private ImageRes _ImageRes;
        public ImageRes ImageRes
        {
            get
            {
                return _ImageRes;
            }
            set
            {
                _ImageRes = value;

                FileInfo file = new FileInfo(_ImageRes.FullPath);

                BitmapImage image = null;
                using (FileStream fileStream = File.Open(file.FullName, FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader binReader = new BinaryReader(fileStream))
                    {
                        byte[] bytes = binReader.ReadBytes((int)file.Length);

                        image = new BitmapImage();
                        image.CacheOption = BitmapCacheOption.OnLoad;
                        image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                        image.BeginInit();
                        image.StreamSource = new MemoryStream(bytes);
                        image.EndInit();
                    }
                }

                ImageSource = image;
            }
        }

        private ImageSource _ImageSource;
        public ImageSource ImageSource
        {
            get
            {
                return _ImageSource;
            }
            private set
            {
                _ImageSource = value;
                NotifyPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName]string propertyName = "")
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
